package eppic.pkg;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Project {
	
	public int id;
	public String name;
	public int client_id;
	public float rate;
	
	public Project()
	{
		this.name = "";
		this.client_id = -1;
		this.rate = 0;
	}
	
	public Project(int id, String name, int client_id, float rate)
	{
		this.id = id;
		this.name = name;
		this.client_id =client_id;
		this.rate = rate;
	}
	
	public Project(String name, int client_id, float rate)
	{
		//this.id = 2;
		this.name = name;
		this.client_id = client_id;
		this.rate = rate;
	}
	
	public Project(int id, Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		
        Cursor c=db.rawQuery("SELECT * FROM projects WHERE _id="+id, null);
        c.moveToFirst();
       
        this.id = id;
    	this.name = c.getString(c.getColumnIndex("name"));
    	this.client_id = c.getInt(c.getColumnIndex("client_id"));
    	this.rate = c.getFloat(c.getColumnIndex("rate"));

        c.close();
        db.close();
	}
	
	public String getClientName(Context context)
	{
		String name = "none";
		DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor c=db.rawQuery("SELECT * FROM clients WHERE _id = "+this.client_id+" LIMIT 1", null);
       
        int count = c.getCount();
        
        c.moveToFirst();
        if(count > 0)
        {
        	name = c.getString(c.getColumnIndex("company_name"));
        }
        	
        
        c.close();
        db.close();
		
		return name;
	}
	
	public boolean save(Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues cv=new ContentValues();
        
        //Values
		cv.put("name", this.name);
		cv.put("client_id", this.client_id);
		cv.put("rate", this.rate);
		
		if(db.insert("projects", "client_id", cv) != -1)
		{
			db.close();
			return true;
		}
		else
		{
			db.close();
			return false;
		}
	
	}
	
	
	public boolean update(Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues cv=new ContentValues();
        
        //Values
		cv.put("name", this.name);
		cv.put("client_id", this.client_id);
		cv.put("rate", this.rate);

		
		if(db.update("projects", cv, "_id="+this.id, null) != -1)
		{
			db.close();
			return true;
		}
		else
		{
			db.close();
			return false;
		}
	}
	
	public boolean delete(Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        
        try{
        	db.execSQL("DELETE FROM projects WHERE _id="+this.id);
        }catch(Exception e)
        {
        	return false;
        }
        
        try{
        	db.execSQL("DELETE FROM logs WHERE parent_id="+this.id+" AND is_project = 1");
        }catch(Exception e)
        {
        	return false;
        }
        
        try{
        	db.execSQL("DELETE FROM expenses WHERE parent_id="+this.id+" AND is_project = 1");
        }catch(Exception e)
        {
        	return false;
        }
        
		return true;
	}
	
	public Log[] getLogs(Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor c=db.rawQuery("SELECT * FROM logs WHERE parent_id = "+this.id+" AND is_project = 1", null);
       
        int count = c.getCount();
        Log[] all = new Log[count];
        
        c.moveToFirst();
        if(count > 0)
        {
        	for(int i = 0; i < count; i++)
        	{
	        	int id = c.getInt(c.getColumnIndex("_id"));
	        	int parent_id = c.getInt(c.getColumnIndex("parent_id"));
	        	int is_project = c.getInt(c.getColumnIndex("is_project"));
	        	long start = c.getLong(c.getColumnIndex("start"));
	        	long end = c.getLong(c.getColumnIndex("end")); 	
	        	
	        	Log temp_log = new Log(id, parent_id, is_project, start, end);
	            all[i] = temp_log;
	            c.moveToNext();
        	}
        }
        
        c.close();
        db.close();

		return all;
		
	}
	
	public Expense[] getExpenses(Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor c=db.rawQuery("SELECT * FROM expenses WHERE parent_id = "+this.id+" AND is_project = 1", null);
       
        int count = c.getCount();
        Expense[] all = new Expense[count];
        
        c.moveToFirst();
        if(count > 0)
        {
        	for(int i = 0; i < count; i++)
        	{
	        	int id = c.getInt(c.getColumnIndex("_id"));
	        	int parent_id = c.getInt(c.getColumnIndex("parent_id"));
	        	int is_project = c.getInt(c.getColumnIndex("is_project"));
	        	String name = c.getString(c.getColumnIndex("name"));
	        	float cost = c.getFloat(c.getColumnIndex("cost")); 	
	        	
	        	Expense temp_expense = new Expense(id, parent_id, is_project, name, cost);
	            all[i] = temp_expense;
	            c.moveToNext();
        	}
        }
        
        c.close();
        db.close();

		return all;
		
	}
	
	
}
